// pages/mine/mine.js
import {
  getLoginStatus,
  getUserFollows,
  getUserFolloweds,
  getVipInfo,
  getUserLevel,
  getUserPlaylist,
  getUserDj,
  getUserEvent,

} from '../../api/index'
import {
  millisecondsToYears
} from '../../utils/isLogin'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    isLogin: false,
    tabIndex: 0,
    tabContent: [],
    titleTable: [],
    musicList: [],
    podcastsList: [],
    dynamicList: [],
    userPlaylist: [],
    follows: {},
    followeds: {},
    vipInfo: {},
    userLevel: {},
    userDjList: [],
    userEventList: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      isLogin: !!wx.getStorageSync('userInfo')
    })
  },

  onShow(){
    this.setData({
      isLogin: !!wx.getStorageSync('userInfo')
    })
    this.getTabContent()
    this.getTitleTabs()
    this.getMusicList()
    this.getPodcastsList()
    this.getDynamicList()
    this.getLoginStatus()
  },

  getTabContent() {
    const result = [
      {
        id: 0,
        label: '音乐',
        name: '',
        isActive:true,
      },
      {
        id: 1,
        label: '博客',
        name: '',
        isActive:false
      },
      {
        id: 2,
        label: '动态',
        name: '',
        isActive:false
      },
    ]
    this.setData({tabContent: result})
  },
  onTabChange(event) {
    this.setData({
      tabIndex: event.detail.index,
    })
  },
  getTitleTabs() {
    const titleTable = [
      {
        id: 0,
        title: '最近',
        icon: 'icon-recently'
      },
      {
        id: 1,
        title: '本地',
        icon: 'icon-file'
      },
      {
        id: 2,
        title: '云盘',
        icon: 'icon-cloud'
      }
    ]
    this.setData({ titleTable })
  },
  getMusicList() {
    const musicList = [
      {
        index: 1,
        musicTitle: '创建歌单',
        musicName: '',
        imageUrl: '/static/images/mine/user-bg.jpg',
        icon: 'icon-create',
        isFlag: true,
      },
      {
        index: 2,
        musicTitle: '导入外部歌单',
        musicName: '轻松导入其他App的歌单',
        imageUrl: '/static/images/mine/user-bg.jpg',
        icon: 'icon-export',
        isFlag: true,
      }
    ]
    this.setData({ musicList })
  },
  getPodcastsList() {
    const podcastsList = [
      {
        index: 0,
        title: '添加播客和有声书',
        name: '',
        imageUrl :'/static/images/mine/user-bg.jpg',
        icon: 'icon-create'
      }
    ]
    this.setData({ podcastsList })
  },
  getDynamicList() {
    const dynamicList = []
    this.setData({ dynamicList })
  },
  getLogin() {
    wx.navigateTo({
      url: '/pages/login/index?from=mine'
    })
  },
  async getLoginStatus() {
    const isUser = wx.getStorageSync('userInfo') || ''
    const userInfo = isUser && JSON.parse(isUser)
    this.setData({ userInfo })
    const { userId } = userInfo
    if (!isUser) {
      const { data: {profile, code }} = await getLoginStatus()
      if (code === 200) {
        this.setData({ userInfo: profile})
      }
    } else {
      Promise.all([
            getUserFollows({uid: userId}),
            getUserFolloweds({uid: userId}),
            getVipInfo({uid: userId}),
            getUserLevel(),
            getUserPlaylist({uid: userId}),
            getUserDj({uid: userId}),
            getUserEvent({uid: userId})

          ]
      ).then(res => {
        const [
          follows,
          followeds,
          vipInfo,
          userLevel,
          userPlaylist,
          userDj,
          userEvent,

        ] = res
        this.setData({
          follows:  follows.follow,
          followeds: followeds.followeds,
          vipInfo: vipInfo.data,
          userLevel: userLevel.data,
          userPlaylist: (userPlaylist.playlist||[]).concat(this.data.musicList),
          userDjList: userDj.programs.concat(this.data.podcastsList),
          userEventList: userEvent.events
        })
      })
    }
  }
})